package com.foreveross.taskservice.application.impl.pages;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.dayatang.querychannel.Page;
import org.dayatang.utils.Assert;
import org.springframework.stereotype.Component;

import com.foreveross.taskservice.application.impl.BaseJpaApplicationImpl;
import com.foreveross.taskservice.application.impl.Util.TaskModelSimplifyUtil;
import com.foreveross.taskservice.application.pages.IPageTaskCacheQueryApplication;
import com.foreveross.taskservice.application.pages.vos.BaseQueryVO;
import com.foreveross.taskservice.application.task.statsdetail.vo.BaseTaskVO;
import com.foreveross.taskservice.application.task.statsdetail.vo.TaskServicePage;
import com.foreveross.taskservice.common.CacheDataSpace;
import com.foreveross.taskservice.common.TaskTypeConst;
import com.foreveross.taskservice.common.model.AbstractTaskModel;
import com.foreveross.taskservice.common.model.airfreight.AirFreightQueryConditionConst;
import com.foreveross.taskservice.taskcache.ITaskCacheQuery;
import com.foreveross.taskservice.taskcache.QueryCondition;
import com.foreveross.taskservice.taskcache.QueryResult;
@Component
public class PageTaskCacheQueryApplicationImpl extends BaseJpaApplicationImpl implements IPageTaskCacheQueryApplication{
	@Resource(name="redisTaskCacheQueryImpl")
	private ITaskCacheQuery taskCacheQuery;
	
	@Override
	public Page<BaseTaskVO> query(BaseQueryVO query) throws Exception {
		// TODO Auto-generated method stub
		//目前在查询的时候需要提供数据空间，后期可以去掉或者支持全领域的查询
		Assert.notNull(query, "查询条件不能为空");
		Assert.isTrue(NumberUtils.isNumber(query.getTaskType()),
				String.format("任务类型[%s] 不是合法数字", query.getTaskType()));
		Assert.notNull(query.getCacheSpaceDate(),"数据空间日期不能为空");
		Assert.isTrue(StringUtils.isBlank(query.getTaskStatus()) || NumberUtils.isNumber(query.getTaskStatus()),
				String.format("任务状态[%s] 不是合法数字", query.getTaskStatus()));
		Assert.isTrue(StringUtils.isBlank(query.getChannelId()) || NumberUtils.isNumber(query.getChannelId()),
				String.format("渠道[%s] 不是合法数字", query.getChannelId()));
		TaskServicePage<BaseTaskVO> page = null;
		QueryCondition qd=new QueryCondition();
		if(Integer.valueOf(query.getTaskType())==TaskTypeConst.AIRFREIGHT_TASKTYPE){
			notBlankPut(AirFreightQueryConditionConst.CHANNELCODE,query.getChannelId(),qd);
			notBlankPut(AirFreightQueryConditionConst.FROMCITYCODE,query.getFrom(),qd);
			notBlankPut(AirFreightQueryConditionConst.TOCITYCODE,query.getTo(),qd);
			notBlankPut(AirFreightQueryConditionConst.FLIGHTDATE,query.getFlightDate(),qd);
			notBlankPut(AirFreightQueryConditionConst.RETURDATE,query.getReturnDate(),qd);
			notBlankPut(AirFreightQueryConditionConst.STATUS,query.getTaskStatus(),qd);
			QueryResult result=taskCacheQuery.query(new CacheDataSpace(query.getCacheSpaceDate(),
					Integer.valueOf(query.getTaskType())), qd, query.getPageLen());
			TaskModelSimplifyUtil.cleanActions(result.getResult());
			page=new TaskServicePage<BaseTaskVO>(
					result.getOffset(), result.getSum(),query.getPageLen(), PageVOBuilder.buildTaskModesl2TaskVOs(result.getResult()));
			page.setResultId(result.getResultId());
			page.setResultExpire(result.getResultExpires());
		}
		return page;
	}

	
	private void notBlankPut(String queryKey,String queryValue,QueryCondition qd){
		if(StringUtils.isNotBlank(queryKey) && StringUtils.isNotBlank(queryValue)){
			qd.put(queryKey, queryValue);
		}
	}


	@Override
	public Page<BaseTaskVO> query(String resultId, int offset, int count)
			throws Exception {
		QueryResult result=taskCacheQuery.query(resultId, offset, count);
		TaskServicePage<BaseTaskVO>  page=new TaskServicePage<BaseTaskVO>(
				result.getOffset(), result.getSum(),count, PageVOBuilder.buildTaskModesl2TaskVOs(result.getResult()));
		page.setResultId(result.getResultId());
		page.setResultExpire(result.getResultExpires());
		return page;
	}


	@Override
	public BaseTaskVO query(String taskId) throws Exception {
		BaseTaskVO vo;
		AbstractTaskModel model=taskCacheQuery.query(taskId);
		vo= model==null?new BaseTaskVO():new BaseTaskVO(model);
		return vo;
	}
	
}
